package game.util;

import game.Constants;
import game.Person;
import game.screen.ProfileScreen;
import game.util.InterestUtil.Interest;
import game.util.TraitUtil.Trait;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ProfileUtil {

	public ProfileScreen generateProfile(Person person) {
		final String profileHeader = getRandomProfileHeader(person);
		final String profileBody = generateProfileBody(person);

		return new ProfileScreen(profileHeader, profileBody, person);
	}

	private String generateProfileBody(Person person) {
		String body = "";
		final Collection<Interest> interests = person.getInterests();
		final Collection<Trait> traits = person.getTraits();
		while ((interests.size() > 0) && (traits.size() > 0)) {
			String sentence = "";
			int random = Util.GetRandomIntBetween(0, 1);
			if (random == 0 && interests.size() > 0) {
				final Interest interest = interests.iterator().next();

				String prompt = getRandomInterestPrompt();
				sentence += prompt;

				String verb = Util.GetRandomListValue(interest.getVerbs());
				if (!verb.trim().isEmpty())
					sentence += " " + verb;
				else
					sentence += verb;

				String description = interest.getName();
				sentence += " " + description;

				interests.remove(interest);
			} else if (random == 1 && traits.size() > 0) {
				final Trait trait = traits.iterator().next();

				String description = trait.getDescription();
				String prompt = getRandomTraitPrompt();
				sentence = prompt + " " + description;

				traits.remove(trait);
			}
			if (!sentence.isEmpty())
				body += sentence + ". ";
		}
		return body;
	}

	private String getRandomProfileHeader(Person person) {
		List<String> headers = Arrays.asList(Constants.PROFILE_HEADER);
		String header = Util.GetRandomListValue(headers);
		return header;
	}

	private String getRandomInterestPrompt() {
		List<String> interestPrompts = Arrays.asList(Constants.INTEREST_PROMPT);
		String interestPrompt = Util.GetRandomListValue(interestPrompts);
		return interestPrompt;
	}

	private String getRandomTraitPrompt() {
		List<String> traitPrompts = Arrays.asList(Constants.TRAIT_PROMPT);
		String traitPrompt = Util.GetRandomListValue(traitPrompts);
		return traitPrompt;
	}
}
